Timer = {t = 0, max = 1, isMultiplied = false, fn = function() end }

function Timer:new(t,max,fn,settings)
    settings = settings or {}
    local timer = {t = t, max = max, fn = fn, 
                    isMultiplied = settings.isMultiplied or false,
                    isRepeat = settings.isRepeat or false,
                    isStopped = false} or Timer
    setmetatable(timer, self)
    self.__index = self
    return timer
end

function Timer.update(self,dt)
    if self.stopped then return end

    if self.t > 0 then
        if self.isMultiplied then
            self.t = self.t - dt * speedMultiplier
        else
            self.t = self.t - dt 
        end
    elseif not self.isStopped then
        self.isStopped = true
        self.fn()
        if self.isRepeat then
            Timer.start(self)
        end
    end
end

function Timer.start(self)
    self.isStopped = false
    self.t = self.max
end

function Timer.reset(self)
    if not self.isRepeat then
        self.isStopped = true
        self.t = 0
    else
        self.isStopped = false
        self.t = self.max
    end
end

function Timer.stop(self)
    self.isStopped = true
    self.t = self.max
end

function Timer.draw(self, count, color)
    if self.isStopped then return count end
    count = count + 1 
    lg.setColor(color)
    local angle = (self.t/self.max) * (math.pi * 2)
    lg.arc("fill","pie", 15, lg.getHeight() - 30 * count, 10, -math.pi/2, angle - math.pi / 2)
    lg.setColor(1,1,1)
    return count
end


return Timer

